package com.library.controller;

import com.library.dto.NotificationDTO;
import com.library.service.NotificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通知控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/notifications")
@RequiredArgsConstructor
public class NotificationController {
    private final NotificationService notificationService;

    /**
     * 获取通知列表接口
     * GET /api/notifications
     */
    @GetMapping
    public ResponseEntity<Map<String, List<NotificationDTO>>> getNotifications(
            @RequestAttribute Long userId) {
        List<NotificationDTO> notifications = notificationService.getNotifications(userId);
        Map<String, List<NotificationDTO>> response = new HashMap<>();
        response.put("notifications", notifications);
        return ResponseEntity.ok(response);
    }

    /**
     * 标记为已读接口
     * PUT /api/notifications/{id}/read
     */
    @PutMapping("/{id}/read")
    public ResponseEntity<Map<String, String>> markAsRead(
            @PathVariable Long id,
            @RequestAttribute Long userId) {
        notificationService.markAsRead(userId, id);
        Map<String, String> response = new HashMap<>();
        response.put("message", "标记为已读成功");
        return ResponseEntity.ok(response);
    }

    /**
     * 删除通知接口
     * DELETE /api/notifications/{id}
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, String>> deleteNotification(
            @PathVariable Long id,
            @RequestAttribute Long userId) {
        notificationService.deleteNotification(userId, id);
        Map<String, String> response = new HashMap<>();
        response.put("message", "删除通知成功");
        return ResponseEntity.ok(response);
    }

    /**
     * 获取未读通知数量接口
     * GET /api/notifications/unread-count
     */
    @GetMapping("/unread-count")
    public ResponseEntity<Map<String, Long>> getUnreadCount(
            @RequestAttribute Long userId) {
        long count = notificationService.getUnreadCount(userId);
        Map<String, Long> response = new HashMap<>();
        response.put("count", count);
        return ResponseEntity.ok(response);
    }
}

